$(function() {
	var innerID;
	var userTable;
	var orderTable;
	var rebateTable;
	var childRebateTable;
	var allChildRebateTable;
	
	paramsGet();
	cnamzParamsGet();
	jpamzParamsGet();
	
	$("#rateSet-shopSite").change(function() {
		paramsGet();
	});
	
	$("#cnamzRateSet-shopSite").change(function() {
		cnamzParamsGet();
	});
	
	$("#jpamzRateSet-shopSite").change(function() {
		jpamzParamsGet();
	});
	
	var url = ctxPath + "/sys/user/getUser.do";
	doGetReq(url, {}, function(rsp) {
		var retData = rsp.retData;
		innerID = retData.innerID;
	});
	
	doGetReq(ctxPath + "/sys/notice/queryNotice.do", {}, function(rsp) {
		var retData = rsp.retData;
		var content = retData.content;
		var status = retData.status;
		if(content != undefined && content != null && content != "") {
			$("#noticeDiv p:first").html(content);
			if("1"==status) {
				$("#pubNoticeBtn").attr("disabled", "disabled");
				$("#cancelNoticeBtn").removeAttr("disabled");
			} else if("2"==status || "3"==status) {
				$("#pubNoticeBtn").removeAttr("disabled");
				$("#cancelNoticeBtn").attr("disabled", "disabled");
			}
			$("#noticeDiv").show();
			$("#noticeForm").hide();
		} else{
			$("#noticeDiv").hide();
			$("#noticeForm").show();
		}
	});
	
	$("#rateSetBtn").on('click', function() {
		var url = ctxPath + "/sys/order/paramsSet.do";
		var shopSite = $("#rateSet-shopSite").val();
		var rate = $("#rateSet-rate").val();
		var rebateRate = $("#rateSet-rebateRate").val();
		var parentRebateMonth = $("#rateSet-parentRebateMonth").val();
		var parentRebateRate = $("#rateSet-parentRebateRate").val();
		if(rate == "" || rebateRate == "") {
			alert("参数不能为空.");
			return;
		}
		var data = {"shopSite":shopSite,"rate":rate,"rebateRate":rebateRate,"parentRebateMonth":parentRebateMonth,"parentRebateRate":parentRebateRate};
		doPostReq(url, data, function(rsp) {
			alert("设置成功");
		});
	});
	
	$("#cnamzRateSetBtn").on('click', function() {
		var url = ctxPath + "/sys/order/paramsSet.do";
		var shopSite = $("#cnamzRateSet-shopSite").val();
		var rebateRate = $("#cnamzRateSet-rebateRate").val();
		if(rebateRate == "") {
			alert("参数不能为空.");
			return;
		}
		var data = {"shopSite":shopSite,"rebateRate":rebateRate};
		doPostReq(url, data, function(rsp) {
			alert("设置成功");
		});
	});
	
	$("#jpamzRateSetBtn").on('click', function() {
		var url = ctxPath + "/sys/order/paramsSet.do";
		var shopSite = $("#jpamzRateSet-shopSite").val();
		var rebateRate = $("#jpamzRateSet-rebateRate").val();
		var convertRate = $("#jpamzRateSet-convertRate").val();
		if(rebateRate == "") {
			alert("参数不能为空.");
			return;
		}
		var data = {"shopSite":shopSite,"rebateRate":rebateRate,"convertRate":convertRate};
		doPostReq(url, data, function(rsp) {
			alert("设置成功");
		});
	});
	
	$("#exportExecl").bind("click", function() {
		exportExecl();
	});
	
	$("#exportRebateData").bind("click", function() {
		exportAllRebateExecl();
	});
	
	$("#exportAllChildRebateData").bind("click", function() {
		exportAllChildRebateExecl();
	});
	
	$('#myTabs a').click(function(e) {
		e.preventDefault();
		$(this).tab('show');
	});
	
	$("#pubNoticeBtn").bind("click", function() {
		pubNotice();
	});
	
	$("#cancelNoticeBtn").bind("click", function() {
		cancelNotice();
	});
	
	$("#editNoticeBtn").bind("click", function() {
		editNotice();
	});
	
	$('.input-daterange input').datepicker({
        format: "yyyy-mm-dd",
        clearBtn: true,
        language: "zh-CN",
        autoclose: true,
        endDate: "0d"
    });
	
	$('#createDate').datepicker({
		format: "yyyy-mm-dd",
		clearBtn: true,
		language: "zh-CN",
		autoclose: true
	});
	
	$("#rebate-orderYear").datetimepicker({
		format: 'yyyy',
		language: "zh-CN",
		autoclose: true,
		startView: "4",
		minView: "4",
		maxView: "4"
	});
	
	$("#rebate-orderMonth").datetimepicker({
		format: 'mm',
		language: "zh-CN",
		autoclose: true,
		startView: "3",
		minView: "3",
		maxView: "3"
	});
	
	$("#allChildRebate-orderYear").datetimepicker({
		format: 'yyyy',
		language: "zh-CN",
		autoclose: true,
		startView: "4",
		minView: "4",
		maxView: "4"
	});
	
	$("#allChildRebate-orderMonth").datetimepicker({
		format: 'mm',
		language: "zh-CN",
		autoclose: true,
		startView: "3",
		minView: "3",
		maxView: "3"
	});
	
	$('#importBtn').on('click', function() {
		importReport();
	});
	
	$('#cnamzImportBtn').on('click', function() {
		importCnamzReport();
	});
	
	$('#jpamzImportBtn').on('click', function() {
		importJpamzReport();
	});
	
	$('#compareBtn').on('click', function() {
		compare();
	});
	
	$('#cnamzCompareBtn').on('click', function() {
		compareCnamz();
	});
	
	$('#jpamzCompareBtn').on('click', function() {
		compareJpamz();
	});
	
	$("#queryData").bind("click", function() {
		orderTable = initOrderTable();
		queryAllSum();
	});
	
	$("#child-queryData").bind("click", function() {
		childRebateTable = initChildRebateTable();
		queryAllChildSum();
	});
	
	$("#queryUserData").bind("click", function() {
		userTable = initUserTable();
	});
	
	$("#queryRebateData").bind("click", function() {
		rebateTable = initRebateTable();
	});
	
	$("#queryAllChildRebateData").bind("click", function() {
		allChildRebateTable = initAllChildRebateTable();
	});
	
	$("#queryData").trigger("click");
	$("#child-queryData").trigger("click");
	$("#queryUserData").trigger("click");
	$("#queryRebateData").trigger("click");
	$("#queryAllChildRebateData").trigger("click");
	
	$("#userTable tbody").on("click",".active-btn",function(){
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
            var jqob=$(val);
            if(i == 2) {
            	var innerID = jqob.text();
            	activeUser(innerID);
            }
        });
    });
	
	$("#userTable tbody").on("click",".delete-btn",function(){
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
            var jqob=$(val);
            if(i == 2) {
            	var innerID = jqob.text();
            	deleteUser(innerID);
            }
        });
    });
	
	$("#orderTable tbody").on("click",".edit-btn",function(){
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
            var jqob=$(val);
            if(i == 3) {
            	var txt=jqob.text();
            	var put=$("<input type='text' />");
            	put.val(txt);
            	jqob.html(put);
            }
        });
        $(this).html("保存");
        $(this).toggleClass("edit-btn");
        $(this).toggleClass("save-btn");
    });
	
	$("#orderTable tbody").on("click",".delete-btn",function(){
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
            var jqob=$(val);
            if(i == 1) {
            	var orderID=jqob.text();
            	deleteOrder(orderID);
            }
        });
    });
	
	
	$("#orderTable tbody").on("click",".save-btn",function(){
        var row=orderTable.row($(this).parents("tr"));
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
        	var jqob=$(val);
        	//把input变为字符串
        	if(i == 3) {
        		var txt=jqob.children("input").val();
        		jqob.html(txt);
        		orderTable.cell(jqob).data(txt);//修改DataTables对象的数据
        	}
        });
        var orderID = orderTable.row($(this).parents("tr")).data().orderID;
        var rebate = orderTable.row($(this).parents("tr")).data().rebate;
        var url = ctxPath + "/sys/order/updateOrder.do";
        var data = {"rebate": rebate, "orderID": orderID};
        doPostReq(url, data, function(rsp){
        	alert("修改成功");
        });
        $(this).html("编辑");
        $(this).toggleClass("edit-btn");
        $(this).toggleClass("save-btn");
    });
	
	$('#orderTable tbody').on( 'click', 'tr', function () {
        $(this).toggleClass('selected');
    } );
 
    $('#giveRebate,#cancleRebate').click(function () {
    	var id = $(this).attr("id");
    	var isRebate = "";
    	if("giveRebate"==id) {
    		isRebate = "2";
    	} else{
    		isRebate = "1";
    	}
        var rows = orderTable.rows('.selected').data();
        var orderIDArray = "";
        if(rows.length == 0) {
        	return;
        }
        for(var i = 0;i < rows.length;i++) {
        	orderIDArray = orderIDArray + rows[i].orderID + ",";
        }
        var url = ctxPath + "/sys/order/updateAllOrder.do";
        var data = {"isRebate": isRebate, "orderID": orderIDArray};
        doPostReq(url, data, function(rsp){
        	$("#queryData").trigger("click");
        });
    });
    
    $('#setValid,#setInValid,#setVerify').click(function () {
    	var id = $(this).attr("id");
    	var isValid = "";
    	if("setValid"==id) {
    		isValid = "2";
    	} else if("setInValid"==id){
    		isValid = "1";
    	} else{
    		isValid = "3";
    	}
        var rows = orderTable.rows('.selected').data();
        if(rows.length == 0) {
        	return;
        }
        var orderIDArray = "";
        for(var i = 0;i < rows.length;i++) {
        	orderIDArray = orderIDArray + rows[i].orderID + ",";
        }
        var url = ctxPath + "/sys/order/updateAllOrder.do";
        var data = {"isValid": isValid, "orderID": orderIDArray};
        doPostReq(url, data, function(rsp){
        	$("#queryData").trigger("click");
        });
    });
    
    $("#child-orderTable tbody").on("click",".child-edit-btn",function(){
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
            var jqob=$(val);
            if(i == 3) {
            	var txt=jqob.text();
            	var put=$("<input type='text' />");
            	put.val(txt);
            	jqob.html(put);
            }
        });
        $(this).html("保存");
        $(this).toggleClass("child-edit-btn");
        $(this).toggleClass("child-save-btn");
    });
	
	$("#child-orderTable tbody").on("click",".child-save-btn",function(){
        var row=childRebateTable.row($(this).parents("tr"));
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
        	var jqob=$(val);
        	//把input变为字符串
        	if(i == 3) {
        		var txt=jqob.children("input").val();
        		jqob.html(txt);
        		childRebateTable.cell(jqob).data(txt);//修改DataTables对象的数据
        	}
        });
        var orderID = childRebateTable.row($(this).parents("tr")).data().orderID;
        var parentRebate = childRebateTable.row($(this).parents("tr")).data().parentRebate;
        var url = ctxPath + "/sys/order/updateOrder.do";
        var data = {"parentRebate": parentRebate, "orderID": orderID};
        doPostReq(url, data, function(rsp){
        	alert("修改成功");
        });
        $(this).html("编辑");
        $(this).toggleClass("child-edit-btn");
        $(this).toggleClass("child-save-btn");
    });
	
	$('#child-orderTable tbody').on( 'click', 'tr', function () {
        $(this).toggleClass('selected');
    } );
 
    $('#child-giveParentRebate,#child-cancleParentRebate').click(function () {
    	var id = $(this).attr("id");
    	var isParentRebate = "";
    	if("child-giveParentRebate"==id) {
    		isParentRebate = "2";
    	} else{
    		isParentRebate = "1";
    	}
        var rows = childRebateTable.rows('.selected').data();
        var orderIDArray = "";
        if(rows.length == 0) {
        	return;
        }
        for(var i = 0;i < rows.length;i++) {
        	orderIDArray = orderIDArray + rows[i].orderID + ",";
        }
        var url = ctxPath + "/sys/order/updateAllOrder.do";
        var data = {"isParentRebate": isParentRebate, "orderID": orderIDArray};
        doPostReq(url, data, function(rsp){
        	$("#child-queryData").trigger("click");
        });
    });
    
    
    
    $('#rebateTable tbody').on( 'click', 'tr', function () {
    	if($(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        } else {
        	rebateTable.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    });
    
    $('#rebate-giveRebate,#rebate-cancleRebate').click(function () {
    	var id = $(this).attr("id");
    	var isRebate = "";
    	if("rebate-giveRebate"==id) {
    		isRebate = "2";
    	} else{
    		isRebate = "1";
    	}
        var row = rebateTable.row('.selected').data();
        if(row.length == 0) {
        	return;
        }
        var innerID = row.innerID;
        var orderDate = row.orderDate;
        var url = ctxPath + "/sys/order/updateAllOrderTo.do";
        var data = {"isRebate": isRebate, "innerID": innerID, "orderDate": orderDate};
        doPostReq(url, data, function(rsp){
        	$("#queryRebateData").trigger("click");
        });
    });
    
    $('#allChildRebateTable tbody').on( 'click', 'tr', function () {
    	if($(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        } else {
        	allChildRebateTable.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    });
    
    $('#allChildRebate-giveRebate,#allChildRebate-cancleRebate').click(function () {
    	var id = $(this).attr("id");
    	var isParentRebate = "";
    	if("allChildRebate-giveRebate"==id) {
    		isParentRebate = "2";
    	} else{
    		isParentRebate = "1";
    	}
        var row = allChildRebateTable.row('.selected').data();
        if(row.length == 0) {
        	return;
        }
        var innerID = row.innerID;
        var orderDate = row.orderDate;
        var url = ctxPath + "/sys/order/updateAllOrderTo.do";
        var data = {"isParentRebate": isParentRebate, "innerID": innerID, "orderDate": orderDate};
        doPostReq(url, data, function(rsp){
        	$("#queryAllChildRebateData").trigger("click");
        });
    });
    
    $.fn.bootstrapValidator.validators.same = {
        html5Attributes: {
            message: 'message',
            field: 'field'
        },
        validate: function(validator, $field, options) {
            var value = $field.val();
            if (value == '') {
                return true;
            }

            var compareWith = validator.getFieldElements(options.field);
            if (compareWith == null) {
                return true;
            }

            if (value == compareWith.val()) {
                validator.updateStatus(options.field, validator.STATUS_VALID, 'same');
                return true;
            } else {
                return false;
            }
        }
    };
    
	$('#pwdForm').bootstrapValidator({
		live : 'disabled',
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields : {
			password : {
				validators : {
					notEmpty : {
						message : '旧密码不能为空.'
					}
				}
			},
			newPwd : {
				validators : {
					notEmpty : {
						message : '新密码不能为空.'
					}
				},
				stringLength : {
					min : 6,
					max : 32,
					message : '必须在6~32字符长度之间.'
				}
			},
			confirmPwd : {
				validators : {
					notEmpty : {
						message : '确认密码不能为空.'
					},
					stringLength : {
						min : 6,
						max : 32,
						message : '必须在6~32字符长度之间.'
					},
					same: {
                        field: 'newPwd',
                        message: '确认密码不一致.'
                    },
                    remote: {
						url: ctxPath + '/checkAdminPwd.do',
						data: function(validator) {
							return {
								password: validator.getFieldElements('password').val(),
								innerID: innerID
							};
						},
						message: '旧密码错误.'
					}
				}
			}
		},
		submitHandler: function(validator, form, submitButton) {
			var url = ctxPath + "/changePwd.do";
			var newPwd = $("#newPwd").val();
			var data = {"innerID": innerID, "newPwd": newPwd};
			doPostReq(url, data, function(rsp) {
				alert("修改成功,请重新登录");
				var url = ctxPath + "/sys/user/logout.do";
				var data = {};
				doPostReq(url, data, function(rsp) {
					location.href = ctxPath + "/initAdminLoginPage.do";
				});
			});
		}
	});
	
	$('#noticeForm').bootstrapValidator({
		live : 'disabled',
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields : {
			content : {
				validators : {
					notEmpty : {
						message : '通知内容不能为空.'
					},
					stringLength : {
						min : 1,
						max : 150,
						message : '必须在1~150字符长度之间.'
					}
				}
			}
		},
		submitHandler: function(validator, form, submitButton) {
			var url = ctxPath + "/sys/notice/saveNotice.do";
			var content = $("#content").val();
			var data = {"content": content, "status": "3"};
			doPostReq(url, data, function(rsp) {
				$("#noticeDiv p:first").html(content);
				$("#pubNoticeBtn").removeAttr("disabled");
				$("#cancelNoticeBtn").removeAttr("disabled");
				$("#noticeDiv").show();
				$("#noticeForm").hide();
				alert("保存成功");
			});
		}
	});
	
	$('#pwdSaveBtn').click(function() {
		var password = $("#password").val();
		var newPwd = $("#newPwd").val();
		var confirmPwd = $("#confirmPwd").val();
		if($.trim(password) != "") {
			$("#password").val($.md5(password));
		}
		if($.trim(newPwd) != "") {
			$("#newPwd").val($.md5(newPwd));
		}
		if($.trim(confirmPwd) != "") {
			$("#confirmPwd").val($.md5(confirmPwd));
		}
		$("#pwdForm").submit();
    });
});

function showResponse(rsp) {
	$('#importBtn').button('complete');
	var retCode = rsp.retCode;
	if (retCode == "SUCCESS") {
		alert("导入成功");
	} else {
		alert("导入失败，确认文件大小以及文件格式是否正确");
	}
}

function showCnamzResponse(rsp) {
	$('#cnamzImportBtn').button('complete');
	var retCode = rsp.retCode;
	if (retCode == "SUCCESS") {
		alert("导入成功");
	} else {
		alert("导入失败，确认文件大小以及文件格式是否正确");
	}
}

function showJpamzResponse(rsp) {
	$('#jpamzImportBtn').button('complete');
	var retCode = rsp.retCode;
	if (retCode == "SUCCESS") {
		alert("导入成功");
	} else {
		alert("导入失败，确认文件大小以及文件格式是否正确");
	}
}

function initOrderTable() {
	return $("#orderTable").DataTable({
		"pageLength": 10,
        "bProcessing": true,
        "bServerSide": true,
        "bDestroy" : true,
        "bFilter": false,
        "bLengthChange": false,
        "bAutoWidth" : true,
		"sAjaxDataProp" : "aaData",
		"bSort" : false,
		"sAjaxSource" : ctxPath + "/sys/order/queryAllOrder.do",
        "aoColumns": [
			{"mData": "id", "bVisible": false},
			{"mData": "username"},
			{"mData": "orderID"},
			{"mData": function(order) {
				var shopSite = order.shopSite;
				return getShopSiteName(shopSite);
			}},
			{"mData": "rebateID", "bVisible": false},
			{"mData": "rebate"},
			{"mData": "innerID", "bVisible": false},
			{"mData": "orderPrice"},
			{"mData": "orderDate"},
			{"mData": function(order) {
				var isValid = order.isValid;
				if("2"==isValid) {
					return "有效";
				} else {
					return "待审核";
				}
			}},
			{"mData": function(order) {
				var isRebate = order.isRebate;
				if("1"==isRebate) {
					return "未发放";
				} else if("2"==isRebate) {
					return "已发放";
				}
			}},
			{"mData": function(order) {
				return "<a href='#' class='edit-btn' type='button'>编辑</a>&nbsp;&nbsp;<a href='#' class='delete-btn' type='button'>删除</a>";
			}}],
		"fnServerParams": function(aoData) {
			var username = $("#username").val();
			if(username != "") {
				aoData.push({
					"name" : "username",
					"value" : username
				});
			}
			var orderID = $("#orderID").val();
			if(orderID != "") {
				aoData.push({
					"name" : "orderID",
					"value" : orderID
				});
			}
			var shopSite = $("#shopSite").val();
			if(shopSite != "") {
				aoData.push({
					"name" : "shopSite",
					"value" : shopSite
				});
			}
			var startDate = $("#startDate").val();
			if(startDate != "") {
				aoData.push({
					"name" : "startDate",
					"value" : startDate
				});
			}
			var endDate = $("#endDate").val();
			if(endDate != "") {
				aoData.push({
					"name" : "endDate",
					"value" : endDate
				});
			}
			var isValid = $("#isValid").val();
			if(isValid != "") {
				aoData.push({
					"name" : "isValid",
					"value" : isValid
				});
			}
			var isRebate = $("#isRebate").val();
			if(isRebate != "") {
				aoData.push({
					"name" : "isRebate",
					"value" : isRebate
				});
			}
		},
		"fnServerData": function(sSource, aoData, fnCallback) {
			$.ajax({
				"dataType": 'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": function(resp) {
					var retCode = resp.retCode;
					if (retCode == "SUCCESS") {
						fnCallback(resp.retData);
					}
				}
			});
		},
		"fnDrawCallback": function(table) {  
            $("#orderTable_paginate").append("  到第 <input style='height:28px;line-height:28px;width:40px;' class='margin text-center' id='changePage' type='text'> 页  <a class='btn btn-default shiny' style='margin-bottom:5px' href='javascript:void(0);' id='dataTable-btn'>确认</a>");  
            var oTable = $("#orderTable").dataTable();  
            $('#dataTable-btn').click(function(e) {  
                if($("#changePage").val() && $("#changePage").val() > 0) {  
                    var redirectpage = $("#changePage").val() - 1;  
                } else {  
                    var redirectpage = 0;  
                }  
                oTable.fnPageChange(redirectpage);  
            });  
        }
	});
}

function queryAllSum() {
	var url = ctxPath + "/sys/order/queryAllSum.do";
	var username = $("#username").val();
	var orderID = $("#orderID").val();
	var shopSite = $("#shopSite").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var isValid = $("#isValid").val();
	var isRebate = $("#isRebate").val();
	var data = new Object();
	if(username != "") {
		data.username = username;
	}
	if(orderID != "") {
		data.orderID = orderID;
	}
	if(shopSite != "") {
		data.shopSite = shopSite;
	}
	if(startDate != "") {
		data.startDate = startDate;
	}
	if(endDate != "") {
		data.endDate = endDate;
	}
	if(isValid != "") {
		data.isValid = isValid;
	}
	if(isRebate != "") {
		data.isRebate = isRebate;
	}
	doPostReq(url, data, function(rsp) {
		$("#queryAllSum").html("总佣金：" + rsp.retData.rebate + "元");
	});
}

function initChildRebateTable() {
	return $("#child-orderTable").DataTable({
        "bProcessing": true,
        "bServerSide": true,
        "bDestroy" : true,
        "bFilter": false,
        "bLengthChange": false,
        "bAutoWidth" : true,
		"sAjaxDataProp" : "aaData",
		"bSort" : false,
		"sAjaxSource" : ctxPath + "/sys/order/queryChildRebateList.do",
        "aoColumns": [
			{"mData": "id", "bVisible": false},
			{"mData": "username"},
			{"mData": "orderID"},
			{"mData": function(order) {
				var shopSite = order.shopSite;
				return getShopSiteName(shopSite);
			}},
			{"mData": "rebateID", "bVisible": false},
			{"mData": "parentRebate"},
			{"mData": "innerID", "bVisible": false},
			{"mData": "orderPrice"},
			{"mData": "orderDate"},
			{"mData": function(order) {
				var isParentRebate = order.isParentRebate;
				if("1"==isParentRebate) {
					return "未分成";
				} else if("2"==isParentRebate) {
					return "已分成";
				}
			}},
			{"mData": function(order) {
				return "<a href='#' class='child-edit-btn' type='button'>编辑</a>";
			}}],
		"fnServerParams": function(aoData) {
			var username = $("#child-username").val();
			if(username != "") {
				aoData.push({
					"name" : "username",
					"value" : username
				});
			}
			var orderID = $("#child-orderID").val();
			if(orderID != "") {
				aoData.push({
					"name" : "orderID",
					"value" : orderID
				});
			}
			var shopSite = $("#child-shopSite").val();
			if(shopSite != "") {
				aoData.push({
					"name" : "shopSite",
					"value" : shopSite
				});
			}
			var startDate = $("#child-startDate").val();
			if(startDate != "") {
				aoData.push({
					"name" : "startDate",
					"value" : startDate
				});
			}
			var endDate = $("#child-endDate").val();
			if(endDate != "") {
				aoData.push({
					"name" : "endDate",
					"value" : endDate
				});
			}
			var isParentRebate = $("#child-isParentRebate").val();
			if(isParentRebate != "") {
				aoData.push({
					"name" : "isParentRebate",
					"value" : isParentRebate
				});
			}
		},
		"fnServerData": function(sSource, aoData, fnCallback) {
			$.ajax({
				"dataType": 'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": function(resp) {
					var retCode = resp.retCode;
					if (retCode == "SUCCESS") {
						fnCallback(resp.retData);
					}
				}
			});
		},
		"fnDrawCallback": function(table) {  
            $("#child-orderTable_paginate").append("  到第 <input style='height:28px;line-height:28px;width:40px;' class='margin text-center' id='child-orderTable-changePage' type='text'> 页  <a class='btn btn-default shiny' style='margin-bottom:5px' href='javascript:void(0);' id='child-orderTable-btn'>确认</a>");  
            var oTable = $("#child-orderTable").dataTable();  
            $('#child-orderTable-btn').click(function(e) {  
                if($("#child-orderTable-changePage").val() && $("#child-orderTable-changePage").val() > 0) {  
                    var redirectpage = $("#child-orderTable-changePage").val() - 1;  
                } else {  
                    var redirectpage = 0;  
                }  
                oTable.fnPageChange(redirectpage);  
            });  
        }	
	});
}

function queryAllChildSum() {
	var url = ctxPath + "/sys/order/queryAllSumChildRebate.do";
	var username = $("#child-username").val();
	var orderID = $("#child-orderID").val();
	var shopSite = $("#child-shopSite").val();
	var startDate = $("#child-startDate").val();
	var endDate = $("#child-endDate").val();
	var isParentRebate = $("#child-isParentRebate").val();
	var data = new Object();
	if(username != "") {
		data.username = username;
	}
	if(orderID != "") {
		data.orderID = orderID;
	}
	if(shopSite != "") {
		data.shopSite = shopSite;
	}
	if(startDate != "") {
		data.startDate = startDate;
	}
	if(endDate != "") {
		data.endDate = endDate;
	}
	if(isParentRebate != "") {
		data.isParentRebate = isParentRebate;
	}
	doPostReq(url, data, function(rsp) {
		$("#child-queryAllSumChildRebate").html("总分成：" + rsp.retData.parentRebate + "元");
	});
}

function initUserTable() {
	return $("#userTable").DataTable({
        "bProcessing": true,
        "bServerSide": true,
        "bDestroy" : true,
        "bFilter": false,
        "bLengthChange": false,
        "bAutoWidth" : false,
		"sAjaxDataProp" : "aaData",
		"bSort" : false,
		"sAjaxSource" : ctxPath + "/sys/user/queryUser.do",
        "aoColumns": [
			{"mData": "id", "bVisible": false},
			{"mData": "username"},
			{"mData": "payAccount"},
			{"mData": "innerID"},
			{"mData": function(user) {
				var role = user.role;
				if("1"==role) {
					return "管理员";
				} else if("2"==role) {
					return "普通用户";
				}
			}},
			{"mData": "email"},
			{"mData": function(user) {
				var status = user.status;
				if("1"==status) {
					return "已激活";
				} else if("2"==status) {
					return "未激活";
				}
			}},
			{"mData": "createDate", "bVisible": false},
			{"mData": function(user) {
				var status = user.status;
				if("2"==status) {
					return "<a href='#' class='active-btn' type='button'>激活</a>&nbsp;<a href='#' class='delete-btn' type='button'>删除</a>";
				} else{
					return "<a href='#' class='delete-btn' type='button'>删除</a>";
				}
			}}],
		"fnServerParams": function(aoData) {
			var username = $("#user-username").val();
			if (username != undefined) {
				aoData.push({
					"name" : "username",
					"value" : username
				});
			}
			var email = $("#user-email").val();
			if (email != undefined) {
				aoData.push({
					"name" : "email",
					"value" : email
				});
			}
		},
		"fnServerData": function(sSource, aoData, fnCallback) {
			$.ajax({
				"dataType": 'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": function(resp) {
					var retCode = resp.retCode;
					if (retCode == "SUCCESS") {
						fnCallback(resp.retData);
					}
				}
			});
		},
		"fnDrawCallback": function(table) {  
            $("#userTable_paginate").append("  到第 <input style='height:28px;line-height:28px;width:40px;' class='margin text-center' id='userTable-changePage' type='text'> 页  <a class='btn btn-default shiny' style='margin-bottom:5px' href='javascript:void(0);' id='userTable-btn'>确认</a>");  
            var oTable = $("#userTable").dataTable();  
            $('#userTable-btn').click(function(e) {  
                if($("#userTable-changePage").val() && $("#userTable-changePage").val() > 0) {  
                    var redirectpage = $("#userTable-changePage").val() - 1;  
                } else {  
                    var redirectpage = 0;  
                }  
                oTable.fnPageChange(redirectpage);  
            });  
        }	
	});
}

function initRebateTable() {
	return $("#rebateTable").DataTable({
        "bProcessing": true,
        "bServerSide": true,
        "bDestroy" : true,
        "bFilter": false,
        "bLengthChange": false,
        "bAutoWidth" : false,
		"sAjaxDataProp" : "aaData",
		"bSort" : false,
		"sAjaxSource" : ctxPath + "/sys/order/queryAllRebate.do",
        "aoColumns": [
			{"mData": "username"},
			{"mData": "payAccount"},
			{"mData": "name"},
			{"mData": "rebate"},
			{"mData": "orderDate"},
			{"mData": function(order) {
				var count = order.count;
				var isRebate = order.isRebate;
				if(count >= isRebate) {
					return "未发放";
				} else if(count < isRebate) {
					return "已发放";
				}
			}}],
		"fnServerParams": function(aoData) {
			var username = $("#rebate-username").val();
			if (username != undefined) {
				aoData.push({
					"name" : "username",
					"value" : username
				});
			}
			var orderYear = $("#rebate-orderYear").val();
			if (orderYear != undefined) {
				aoData.push({
					"name" : "orderYear",
					"value" : orderYear
				});
			}
			var orderMonth = $("#rebate-orderMonth").val();
			if (orderMonth != undefined) {
				aoData.push({
					"name" : "orderMonth",
					"value" : orderMonth
				});
			}
		},
		"fnServerData": function(sSource, aoData, fnCallback) {
			$.ajax({
				"dataType": 'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": function(resp) {
					var retCode = resp.retCode;
					if (retCode == "SUCCESS") {
						fnCallback(resp.retData);
					}
				}
			});
		},
		"fnDrawCallback": function(table) {  
            $("#rebateTable_paginate").append("  到第 <input style='height:28px;line-height:28px;width:40px;' class='margin text-center' id='rebateTable-changePage' type='text'> 页  <a class='btn btn-default shiny' style='margin-bottom:5px' href='javascript:void(0);' id='rebateTable-btn'>确认</a>");  
            var oTable = $("#rebateTable").dataTable();  
            $('#rebateTable-btn').click(function(e) {  
                if($("#rebateTable-changePage").val() && $("#rebateTable-changePage").val() > 0) {  
                    var redirectpage = $("#rebateTable-changePage").val() - 1;  
                } else {  
                    var redirectpage = 0;  
                }  
                oTable.fnPageChange(redirectpage);  
            });  
        }	
	});
}

function initAllChildRebateTable() {
	return $("#allChildRebateTable").DataTable({
        "bProcessing": true,
        "bServerSide": true,
        "bDestroy" : true,
        "bFilter": false,
        "bLengthChange": false,
        "bAutoWidth" : false,
		"sAjaxDataProp" : "aaData",
		"bSort" : false,
		"sAjaxSource" : ctxPath + "/sys/order/queryAllChildRebate.do",
        "aoColumns": [
			{"mData": "parentUsername"},
			{"mData": "payAccount"},
			{"mData": "username"},
			{"mData": "parentRebate"},
			{"mData": "orderDate"},
			{"mData": function(order) {
				var count = order.count;
				var isParentRebate = order.isParentRebate;
				if(count >= isParentRebate) {
					return "未发放";
				} else if(count < isParentRebate) {
					return "已发放";
				}
			}}],
		"fnServerParams": function(aoData) {
			var username = $("#allChildRebate-username").val();
			if(username != undefined) {
				aoData.push({
					"name" : "username",
					"value" : username
				});
			}
			var orderYear = $("#allChildRebate-orderYear").val();
			if (orderYear != undefined) {
				aoData.push({
					"name" : "orderYear",
					"value" : orderYear
				});
			}
			var orderMonth = $("#allChildRebate-orderMonth").val();
			if (orderMonth != undefined) {
				aoData.push({
					"name" : "orderMonth",
					"value" : orderMonth
				});
			}
		},
		"fnServerData": function(sSource, aoData, fnCallback) {
			$.ajax({
				"dataType": 'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": function(resp) {
					var retCode = resp.retCode;
					if (retCode == "SUCCESS") {
						fnCallback(resp.retData);
					}
				}
			});
		},
		"fnDrawCallback": function(table) {  
            $("#allChildRebateTable_paginate").append("  到第 <input style='height:28px;line-height:28px;width:40px;' class='margin text-center' id='allChildRebateTable-changePage' type='text'> 页  <a class='btn btn-default shiny' style='margin-bottom:5px' href='javascript:void(0);' id='allChildRebateTable-btn'>确认</a>");  
            var oTable = $("#allChildRebateTable").dataTable();  
            $('#allChildRebateTable-btn').click(function(e) {  
                if($("#allChildRebateTable-changePage").val() && $("#allChildRebateTable-changePage").val() > 0) {  
                    var redirectpage = $("#allChildRebateTable-changePage").val() - 1;  
                } else {  
                    var redirectpage = 0;  
                }  
                oTable.fnPageChange(redirectpage);  
            });  
        }		
	});
}

function compare(){
	var url = ctxPath + "/sys/order/compare.do";
	var data = {};
	var compareBtn = $('#compareBtn').button('loading');
	doPostReq(url, data, function(rsp) {
		var retData = rsp.retData;
		var size = retData.length;
		var valid = 0;
		var invalid = 0;
		for(var i = 0;i < size;i++){
			if(retData[i].isValid == "1") {
				invalid = invalid + 1;
			} else if(retData[i].isValid == "2") {
				valid = valid + 1;
			}
		} 
		$('#compareBtn').button('complete');
		alert("共比对" + size + "条," + valid + "条有效");
	});
}

function compareCnamz(){
	var url = ctxPath + "/sys/order/compareCnamz.do";
	var data = {};
	var compareBtn = $('#cnamzCompareBtn').button('loading');
	doPostReq(url, data, function(rsp) {
		var retData = rsp.retData;
		var size = retData.length;
		var valid = 0;
		var invalid = 0;
		for(var i = 0;i < size;i++){
			if(retData[i].isValid == "1") {
				invalid = invalid + 1;
			} else if(retData[i].isValid == "2") {
				valid = valid + 1;
			}
		} 
		$('#cnamzCompareBtn').button('complete');
		alert("共比对" + size + "条," + valid + "条有效");
	});
}

function compareJpamz(){
	var url = ctxPath + "/sys/order/compareJpamz.do";
	var data = {};
	var compareBtn = $('#jpamzCompareBtn').button('loading');
	doPostReq(url, data, function(rsp) {
		var retData = rsp.retData;
		var size = retData.length;
		var valid = 0;
		var invalid = 0;
		for(var i = 0;i < size;i++){
			if(retData[i].isValid == "1") {
				invalid = invalid + 1;
			} else if(retData[i].isValid == "2") {
				valid = valid + 1;
			}
		} 
		$('#jpamzCompareBtn').button('complete');
		alert("共比对" + size + "条");
	});
}

function importReport() {
	var reportFile = $("#reportFile").val();
	if (reportFile == "") {
		alert("文件为空");
		return;
	}
	var importBtn = $('#importBtn').button('loading');
	var options = {
		url : ctxPath + "/sys/report/importExcel.do",
		resetForm : true,
		success : showResponse
	};
	$("#importForm").ajaxForm(options);
	$("#importForm").submit();
}

function importCnamzReport() {
	var reportFile = $("#cnamzReportFile").val();
	if (reportFile == "") {
		alert("文件为空");
		return;
	}
	var importBtn = $('#cnamzImportBtn').button('loading');
	var options = {
		url : ctxPath + "/sys/report/importCnamzExcel.do",
		resetForm : true,
		success : showCnamzResponse
	};
	$("#importCnamzForm").ajaxForm(options);
	$("#importCnamzForm").submit();
}

function importJpamzReport() {
	var reportFile = $("#jpamzReportFile").val();
	if (reportFile == "") {
		alert("文件为空");
		return;
	}
	var importBtn = $('#jpamzImportBtn').button('loading');
	var options = {
		url : ctxPath + "/sys/ryreport/importJpamzExcel.do",
		resetForm : true,
		success : showJpamzResponse
	};
	$("#importJpamzForm").ajaxForm(options);
	$("#importJpamzForm").submit();
}

function activeUser(innerID) {
	var url = ctxPath + "/activate.do";
	var data = {"innerID": innerID};
	doPostReq(url, data, function() {
		$("#queryUserData").trigger("click");
	});
}

function deleteUser(innerID) {
	var url = ctxPath + "/sys/user/deleteUser.do";
	var data = {"innerID": innerID};
	if(confirm("确认要删除该用户吗?")){
		if(confirm("请再次确认要删除该用户吗?")){
			doPostReq(url, data, function() {
				$("#queryUserData").trigger("click");
			});
		}
    }
}

function deleteOrder(orderID) {
	var url = ctxPath + "/sys/order/deleteOrder.do";
	var data = {"orderID": orderID};
	if(confirm("确认要删除该订单吗吗?")){
		if(confirm("请再次确认要删除该订单吗?")){
			doPostReq(url, data, function() {
				$("#queryData").trigger("click");
			});
		}
    }
}

//数据导出
function exportExecl() {
	// 创建Form  
    var form = $('<form>');  
    // 设置属性                       
    var url = ctxPath + '/sys/order/exportAllOrderData.do';
    form.attr("style","display:none");
    form.attr('target', '');
    form.attr('method', 'post');  
    
    // 创建Input  
    var username_input = $('<input type="hidden" name="username" />');  
    var username = $("#username").val();
    if(username != "") {
    	username_input.attr('value', username);
    	form.append(username_input);  
    }
    // 创建Input  
    var orderID_input = $('<input type="hidden" name="orderID" />');  
    var orderID = $("#orderID").val();
    if(orderID != "") {
    	orderID_input.attr('value', orderID);
    	form.append(orderID_input);  
    }
    // 创建Input  
    var shopSite_input = $('<input type="hidden" name="shopSite" />');  
    var shopSite = $("#shopSite").val();
    if(shopSite != "") {
    	shopSite_input.attr('value', shopSite);
    	form.append(shopSite_input);  
    }
    // 创建Input  
    var startDate_input = $('<input type="hidden" name="startDate" />');  
    var startDate = $("#startDate").val();
    if(startDate != "") {
    	startDate_input.attr('value', startDate);  
    	form.append(startDate_input); 
    }
    // 创建Input  
    var endDate_input = $('<input type="hidden" name="endDate" />');  
    var endDate = $("#endDate").val();
    if(endDate != "") {
    	endDate_input.attr('value', endDate);  
    	form.append(endDate_input); 
    }
    // 创建Input  
    var isValid_input = $('<input type="hidden" name="isValid" />');  
    var isValid = $("#isValid").val();
    if(isValid != "") {
    	isValid_input.attr('value', isValid);  
    	form.append(isValid_input); 
    }
    // 创建Input  
    var isRebate_input = $('<input type="hidden" name="isRebate" />');  
    var isRebate = $("#isRebate").val();
    if(isRebate != "") {
    	isRebate_input.attr('value', isRebate);  
    	form.append(isRebate_input); 
    }
    form.attr('action', url);
    $('body').append(form);
    // 提交表单  
    form.submit();
}

//数据导出
function exportAllRebateExecl() {
	// 创建Form  
    var form = $('<form>');
    // 设置属性                       
    var url = ctxPath + '/sys/order/exportAllRebateData.do';
    form.attr("style","display:none");
    form.attr('target', '');
    form.attr('method', 'post');
    
    // 创建Input  
    var username_input = $('<input type="hidden" name="username" />');
    var username = $("#rebate-username").val();
    if(username != "") {
    	username_input.attr('value', username);
    	form.append(username_input);  
    }
    // 创建Input  
    var orderYear_input = $('<input type="hidden" name="orderYear" />');
    var orderYear = $("#rebate-orderYear").val();
    if(orderYear != "") {
    	orderYear_input.attr('value', orderYear);
    	form.append(orderYear_input);  
    }
    // 创建Input  
    var orderMonth_input = $('<input type="hidden" name="orderMonth" />');
    var orderMonth = $("#rebate-orderMonth").val();
    if(orderMonth != "") {
    	orderMonth_input.attr('value', orderMonth);  
    	form.append(orderMonth_input); 
    }
    form.attr('action', url);
    $('body').append(form);
    // 提交表单  
    form.submit();
}

//数据导出
function exportAllChildRebateExecl() {
	// 创建Form  
    var form = $('<form>');
    // 设置属性                       
    var url = ctxPath + '/sys/order/exportAllChildRebateData.do';
    form.attr("style","display:none");
    form.attr('target', '');
    form.attr('method', 'post');
    
    // 创建username的input
    var username_input = $('<input type="hidden" name="username" />');
    var username = $("#allChildRebate-username").val();
    if(username != "") {
    	username_input.attr('username', username);
    	form.append(username_input);
    }
    
    // 创建Input  
    var orderYear_input = $('<input type="hidden" name="orderYear" />');
    var orderYear = $("#allChildRebate-orderYear").val();
    if(orderYear != "") {
    	orderYear_input.attr('value', orderYear);
    	form.append(orderYear_input);  
    }
    // 创建Input  
    var orderMonth_input = $('<input type="hidden" name="orderMonth" />');
    var orderMonth = $("#allChildRebate-orderMonth").val();
    if(orderMonth != "") {
    	orderMonth_input.attr('value', orderMonth);  
    	form.append(orderMonth_input); 
    }
    form.attr('action', url);
    $('body').append(form);
    // 提交表单  
    form.submit();
}

function pubNotice() {
	var url = ctxPath + "/sys/notice/updateNotice.do";
	var data = {"status": "1"};
	doPostReq(url, data, function(rsp) {
		$("#pubNoticeBtn").attr("disabled", "disabled");
		$("#cancelNoticeBtn").removeAttr("disabled");
		alert("发布成功");
	});
}

function cancelNotice() {
	var url = ctxPath + "/sys/notice/updateNotice.do";
	var data = {"status": "2"};
	doPostReq(url, data, function(rsp) {
		$("#pubNoticeBtn").removeAttr("disabled");
		$("#cancelNoticeBtn").attr("disabled", "disabled");
		alert("撤消成功");
	});
}

function editNotice() {
	var notice = $("#noticeDiv p:first").html();
	$("#content").val(notice);
	$("#noticeDiv").hide();
	$("#noticeForm").show();
}

function paramsGet() {
	var url = ctxPath + "/sys/order/paramsGet.do";
	var shopSite = $("#rateSet-shopSite").val();
	var data = {"shopSite": shopSite};
	doPostReq(url, data, function(rsp) {
		$("#rateSet-rate").val(rsp.retData.rate);
		$("#rateSet-rebateRate").val(rsp.retData.rebateRate);
		$("#rateSet-convertRate").val(rsp.retData.convertRate);
		$("#rateSet-parentRebateMonth").val(rsp.retData.parentRebateMonth);
		$("#rateSet-parentRebateRate").val(rsp.retData.parentRebateRate);
	});
}

function cnamzParamsGet() {
	var url = ctxPath + "/sys/order/paramsGet.do";
	var shopSite = $("#cnamzRateSet-shopSite").val();
	var data = {"shopSite": shopSite};
	doPostReq(url, data, function(rsp) {
		$("#cnamzRateSet-rebateRate").val(rsp.retData.rebateRate);
	});
}

function jpamzParamsGet() {
	var url = ctxPath + "/sys/order/paramsGet.do";
	var shopSite = $("#jpamzRateSet-shopSite").val();
	var data = {"shopSite": shopSite};
	doPostReq(url, data, function(rsp) {
		$("#jpamzRateSet-convertRate").val(rsp.retData.convertRate);
		$("#jpamzRateSet-rebateRate").val(rsp.retData.rebateRate);
	});
}